/*     */ package com.newcrm.webapi;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CompanyExtManager;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerCategoryManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.NoteManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.db.UserVOIPManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.ParameterUtil;
/*     */ import com.newcrm.util.Util;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.URLEncoder;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Map;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.Cookie;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.Jsoup;
/*     */ 
/*     */ public abstract class AbstractHttpServlet extends HttpServlet
/*     */ {
/*  46 */   private static Logger logger = Logger.getLogger(AbstractHttpServlet.class
/*  47 */     .getName());
/*     */   private static final long serialVersionUID = 5647723579197878509L;
/*     */ 
/*     */   protected abstract String getTagkey();
/*     */ 
/*     */   protected abstract AbstractAction getAddAction();
/*     */ 
/*     */   protected abstract AbstractAction getListAction();
/*     */ 
/*     */   protected abstract AbstractAction getShowAddPageAction();
/*     */ 
/*     */   protected abstract AbstractAction getUpdateAction(String paramString);
/*     */ 
/*     */   protected abstract AbstractAction getShowConfirmDestroyAction(String paramString);
/*     */ 
/*     */   protected abstract AbstractAction getDeleteAction(String paramString);
/*     */ 
/*     */   protected abstract AbstractAction getShowAction(String paramString);
/*     */ 
/*     */   protected abstract AbstractAction getShowUpdatePageAction(String paramString);
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  77 */     doGet(request, response);
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  98 */     if (!checkLogin(request)) {
/*  99 */       showLoginPage(request, response);
/* 100 */       return;
/*     */     }
/*     */ 
/* 103 */     String url = request.getRequestURI();
/* 104 */     String[] param = ParameterUtil.getParameters(url, getTagkey());
/*     */     try {
/* 106 */       if ((param == null) || (param.length == 0))
/*     */       {
/* 108 */         String operation = Util.getURLParameter(request
/* 109 */           .getParameter("operation"));
/* 110 */         AbstractAction action = null;
/* 111 */         if ("new".equals(operation))
/*     */         {
/* 113 */           action = getAddAction();
/* 114 */         } else if ("list".equals(operation)) {
/* 115 */           action = getListAction();
/*     */         }
/* 117 */         if (action != null) {
/* 118 */           action.parse(request, response);
/* 119 */           return;
/*     */         }
/* 121 */         return;
/*     */       }
/* 123 */       if (("new".equals(param[0])) && (param.length == 1)) {
/* 124 */         AbstractAction action = getShowAddPageAction();
/* 125 */         if (action != null) {
/* 126 */           action.parse(request, response);
/* 127 */           return;
/*     */         }
/* 129 */         return;
/*     */       }
/*     */ 
/* 132 */       if (param.length == 1)
/*     */       {
/* 134 */         String id = param[0];
/* 135 */         AbstractAction action = getShowAction(id);
/* 136 */         if (action != null) {
/* 137 */           action.parse(request, response);
/* 138 */           return;
/*     */         }
/*     */ 
/* 141 */         return;
/*     */       }
/*     */ 
/* 144 */       if (param.length == 2) {
/* 145 */         String id = param[0];
/* 146 */         String operation = param[1];
/* 147 */         AbstractAction action = null;
/* 148 */         if ("delete".equals(operation))
/* 149 */           action = getDeleteAction(id);
/* 150 */         else if ("edit".equals(operation))
/* 151 */           action = getShowUpdatePageAction(id);
/* 152 */         else if ("update".equals(operation))
/* 153 */           action = getUpdateAction(id);
/* 154 */         else if ("confirm_destroy".equals(operation)) {
/* 155 */           action = getShowConfirmDestroyAction(id);
/*     */         }
/*     */ 
/* 158 */         if (action != null)
/* 159 */           action.parse(request, response);
/*     */       }
/*     */     }
/*     */     catch (NotFoundException e) {
/* 163 */       request.getRequestDispatcher("/errorpage.jsp").forward(request, 
/* 164 */         response);
/*     */     }
/*     */   }
/*     */ 
/*     */   protected void showLoginPage(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 176 */     String random = Util.getURLParameter(request.getParameter("random"));
/* 177 */     if ((random != null) && (random.trim().length() > 0)) {
/* 178 */       response.setContentType("text/html;charset=utf-8");
/* 179 */       PrintWriter out = response.getWriter();
/* 180 */       JSONObject jsonObject = new JSONObject();
/* 181 */       jsonObject.put("error", "2");
/* 182 */       out.println(jsonObject);
/* 183 */       out.flush();
/* 184 */       out.close();
/* 185 */       return;
/*     */     }
/*     */ 
/* 188 */     String url = request.getRequestURI();
/* 189 */     String contextPath = request.getContextPath() + "/";
/* 190 */     if ((url == null) || (url.trim().length() <= 1) || (url.equals(contextPath)) || 
/* 191 */       (url.indexOf("/signin") > 0)) {
/* 192 */       response.sendRedirect(request.getContextPath() + "/signin");
/* 193 */       return;
/*     */     }
/* 195 */     response.sendRedirect(request.getContextPath() + "/signin" + 
/* 196 */       "?url=" + url);
/*     */   }
/*     */ 
/*     */   protected boolean checkLogin(HttpServletRequest request)
/*     */   {
/* 207 */     if (checkSession(request)) {
/* 208 */       return true;
/*     */     }
/*     */ 
/* 211 */     logger.info("session is null");
/*     */ 
/* 213 */     Cookie[] cookies = request.getCookies();
/* 214 */     String sessionKey = Util.getCookieValue(cookies, "sessionKey");
/* 215 */     if ((sessionKey == null) || (sessionKey.trim().length() == 0)) {
/* 216 */       logger.info("session key from cookie is null");
/* 217 */       return false;
/*     */     }
/* 219 */     UserManager manager = new UserManager();
/*     */     try {
/* 221 */       Map result = manager.checkSessionKey(sessionKey);
/* 222 */       if ((result == null) || (result.size() == 0)) {
/* 223 */         logger.info("get user info from db by session key failed");
/* 224 */         return false;
/*     */       }
/* 226 */       String userId = (String)result.get("UserId");
/* 227 */       int companyId = Util.str2Int(result.get("CompanyId"));
/* 228 */       logger.info("get userId :" + userId + " and companyId:" + companyId);
/* 229 */       if ((userId == null) || (userId.trim().length() == 0)) {
/* 230 */         return false;
/*     */       }
/* 232 */       if (companyId <= 0) {
/* 233 */         return false;
/*     */       }
/*     */ 
/* 236 */       Map companys = (Map)getServletContext()
/* 237 */         .getAttribute("companys");
/* 238 */       if (companys == null) {
/* 239 */         return false;
/*     */       }
/*     */ 
/* 242 */       CompanyInfo company = (CompanyInfo)companys.get(Integer.valueOf(companyId));
/* 243 */       if (company == null) {
/* 244 */         return false;
/*     */       }
/*     */ 
/* 247 */       User currentUser = null;
/* 248 */       for (User user : company.users) {
/* 249 */         if (userId.equals(user.userId)) {
/* 250 */           currentUser = user;
/* 251 */           break;
/*     */         }
/*     */       }
/*     */ 
/* 255 */       if (currentUser == null) {
/* 256 */         logger.warn("currentUser is null");
/* 257 */         return false;
/*     */       }
/*     */ 
/*     */       try
/*     */       {
/* 262 */         long totalUserNum = 0L;
/* 263 */         long totalcustomerNum = 0L;
/* 264 */         long totalcustomerCategoryNum = 0L;
/* 265 */         long totalNoteNum = 0L;
/*     */ 
/* 267 */         java.sql.Connection conn = ConnectionManager.getInstance()
/* 268 */           .getConnection();
/*     */         try
/*     */         {
/* 271 */           UserManager userManager = new UserManager();
/* 272 */           totalUserNum = userManager.getTotalNum(conn, companyId);
/*     */ 
/* 275 */           CustomerManager customerManager = new CustomerManager();
/* 276 */           totalcustomerNum = customerManager.getTotalNum(conn, 
/* 277 */             companyId, 0);
/*     */ 
/* 280 */           CustomerCategoryManager customerCategoryManager = new CustomerCategoryManager();
/* 281 */           totalcustomerCategoryNum = customerCategoryManager
/* 282 */             .getTotalNum(conn, companyId);
/*     */ 
/* 285 */           NoteManager noteManager = new NoteManager();
/* 286 */           totalNoteNum = noteManager.getTotalNum(conn, companyId);
/*     */         }
/*     */         finally {
/*     */           try {
/* 290 */             DbUtils.close(conn);
/*     */           } catch (SQLException e) {
/* 292 */             logger.warn("close conn failed", e);
/*     */           }
/*     */         }
/*     */ 
/* 296 */         String webUrl = "http://tj.qikebao.com/log?t=login&name=" + 
/* 297 */           URLEncoder.encode(company.name, "utf-8") + "&ver=" + 
/* 298 */           URLEncoder.encode("3.6.0.2", "utf-8") + "&u=" + 
/* 299 */           totalUserNum + "&c=" + totalcustomerNum + "&ca=" + 
/* 300 */           totalcustomerCategoryNum + "&n=" + totalNoteNum;
/*     */ 
/* 302 */         Jsoup.connect(webUrl)
/* 303 */           .userAgent(
/* 304 */           "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Tablet PC 2.0)")
/* 305 */           .timeout(10000).get();
/*     */       } catch (Exception e) {
/* 307 */         logger.warn("failed", e);
/*     */       }
/*     */ 
/* 310 */       HttpSession session = request.getSession(true);
/* 311 */       session.setAttribute("user", currentUser);
/*     */ 
/* 314 */       UserVOIPManager userVOIPManager = new UserVOIPManager();
/*     */       try {
/* 316 */         Map voip = userVOIPManager
/* 317 */           .getUserVOIPSetting(currentUser.userId);
/*     */ 
/* 319 */         if (voip != null)
/*     */         {
/* 321 */           CompanyExtManager companyExtManager = new CompanyExtManager();
/*     */ 
/* 324 */           String serverIP = companyExtManager.getFieldValue(
/* 325 */             currentUser.companyId, 
/* 326 */             6);
/* 327 */           if (serverIP == null) {
/* 328 */             serverIP = "";
/*     */           }
/* 330 */           voip.put("ServerIP", serverIP);
/*     */ 
/* 333 */           String appId = companyExtManager.getFieldValue(
/* 334 */             currentUser.companyId, 
/* 335 */             4);
/* 336 */           if (appId == null) {
/* 337 */             appId = "";
/*     */           }
/* 339 */           voip.put("AppID", appId);
/*     */         }
/* 341 */         session.setAttribute("voip", voip);
/*     */       } catch (DBException e) {
/* 343 */         logger.warn("get voip info failed");
/*     */       } catch (SQLException e) {
/* 345 */         logger.warn("get voip info failed");
/*     */       }
/*     */ 
/* 348 */       return true;
/*     */     } catch (DBException e) {
/* 350 */       logger.warn("login in by session key failed", e);
/*     */     } catch (SQLException e) {
/* 352 */       logger.warn("login in by session key failed", e);
/*     */     }
/*     */ 
/* 355 */     return false;
/*     */   }
/*     */ 
/*     */   private boolean checkSession(HttpServletRequest request)
/*     */   {
/* 365 */     HttpSession session = request.getSession(false);
/* 366 */     if (session == null) {
/* 367 */       logger.info("session is null");
/* 368 */       return false;
/*     */     }
/* 370 */     Object obj = session.getAttribute("user");
/* 371 */     if (obj == null) {
/* 372 */       logger.info("session is null");
/* 373 */       return false;
/*     */     }
/*     */ 
/* 376 */     if (!(obj instanceof User)) {
/* 377 */       logger.info("session is null");
/* 378 */       return false;
/*     */     }
/* 380 */     return true;
/*     */   }
/*     */ 
/*     */   protected User getLoginUser(HttpServletRequest request) {
/* 384 */     HttpSession session = request.getSession(true);
/* 385 */     return (User)session.getAttribute("user");
/*     */   }
/*     */ 
/*     */   protected CompanyInfo getCompany(int companyId)
/*     */   {
/* 396 */     Map companys = (Map)
/* 397 */       getServletContext().getAttribute("companys");
/* 398 */     if ((companys == null) || (companys.size() == 0)) {
/* 399 */       return null;
/*     */     }
/* 401 */     return (CompanyInfo)companys.get(Integer.valueOf(companyId));
/*     */   }
/*     */ 
/*     */   protected CompanyInfo getCompany(HttpServletRequest request)
/*     */   {
/* 411 */     User u = getLoginUser(request);
/* 412 */     if (u == null) {
/* 413 */       return null;
/*     */     }
/* 415 */     return getCompany(u.companyId);
/*     */   }
/*     */ 
/*     */   protected void showErrorMessage(HttpServletResponse response, String msg)
/*     */     throws IOException
/*     */   {
/* 427 */     response.setContentType("text/html;charset=utf-8");
/* 428 */     PrintWriter out = response.getWriter();
/* 429 */     JSONObject jsonObject = new JSONObject();
/* 430 */     jsonObject.put("error", "1");
/* 431 */     jsonObject.put("msg", msg);
/* 432 */     out.println(jsonObject);
/* 433 */     out.flush();
/* 434 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.AbstractHttpServlet
 * JD-Core Version:    0.6.1
 */